#include "test_virt_at.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "string.h"

#include "cm_mem.h"

unsigned char test_virt_at_buffer[1024];//AT指令返回数据缓存区
int test_virt_at_len = 0;//AT指令返回数据长度
static osThreadId_t Test_AT_TaskHandle = NULL;//OC AT任务句柄
static osEventFlagsId_t test_at_task_flag = NULL; 
int virt_at_ret_flag = 0;

static void test_vir_at_callback(void *param)
{
    //cm_demo_printf("virt_cb param: %s\n",param);
    // if(atoi(param) == 0)
    // {
        osEventFlagsSet(test_at_task_flag, 0x00000001U);  
    // }
	
}

static void test_vir_at_task(void)
{
	int len;
    while(1)
    {
		osEventFlagsWait(test_at_task_flag, 0x00000001U, osFlagsWaitAny, osWaitForever);

        if(test_virt_at_len >=1024)
        {
            cm_demo_printf("virt at overflow\n");
            test_virt_at_len = 0;
        }
        test_virt_at_len +=  cm_virt_at_get(test_virt_at_buffer+test_virt_at_len, 1024-test_virt_at_len);
        virt_at_ret_flag = 1;
        //cm_demo_printf("test_virt_at_len:%d\n",test_virt_at_len);
        osDelay(10);
        cm_demo_printf("%s",test_virt_at_buffer);
    }   
}

void cm_test_virt_at_operation(unsigned char*cmd[],int len)
{
    if(0 == strcmp("INIT",cmd[2]))
    {
        if(NULL == Test_AT_TaskHandle)
        {
            osThreadAttr_t virat_task_attr = {0};
            test_at_task_flag = osEventFlagsNew(0);
            virat_task_attr.name  = "virat_task";
            virat_task_attr.stack_size = 2048;
            Test_AT_TaskHandle = osThreadNew(test_vir_at_task,0,&virat_task_attr);
        }
        cm_demo_printf("cm_virt_at_init ret:%d\n",cm_virt_at_init(test_vir_at_callback));
    }else if(0 == strcmp("DEINIT",cmd[2]))
    {
        cm_virt_at_deinit();
    }else if(0 == strcmp("SEND",cmd[2]))
    {
        test_virt_at_len = 0;
        memset(test_virt_at_buffer,0,1024);
        uint8_t  *p= (uint8_t *)cm_malloc(100);
        memset(p,0,100);
        if(0 == strcmp(cmd[3],"+++"))
        {
            sprintf(p,"%s",cmd[3]); 
        }else
        {
            sprintf(p,"%s\r\n",cmd[3]);
        }
        //cm_demo_printf("p:%s\n",p);
        //cm_demo_printf("test_virt_at_buffer:%s\n",test_virt_at_buffer);
        cm_demo_printf("[VIRT_AT]:%d\n",cm_virt_at_send(p,strlen(p)));
        int l = 0;
        while(virt_at_ret_flag == 0)
        {
           osDelay(5);
           if(l>10)
           {
            break;
           }
           l++;
        }
        cm_demo_printf("test_virt_at_len:%d,virt_at_ret_flag:%d\n",test_virt_at_len,virt_at_ret_flag);
        virt_at_ret_flag = 0; 
        //cm_demo_printf("%s",test_virt_at_buffer);
        cm_free(p);
    }
    else if(0 == strcmp("GET",cmd[2]))
    {
        cm_demo_printf("cm_virt_at_get called in task\n");
    }
    else if(0 == strcmp("HEAP",cmd[2]))
    {
        // cm_demo_printf("cm_virt_at_init ret:%d\n",cm_virt_at_init(test_vir_at_callback));
        // cm_virt_at_deinit();
        // uint8_t  *p= (uint8_t *)cm_malloc(100);
        // cm_free(p);
        test_virt_at_len = 0;
        cm_demo_printf("[VIRT_AT]:%d\n",cm_virt_at_send("AT\r\n",strlen("AT\r\n")));
        int l = 0;
        while(virt_at_ret_flag == 0)
        {
           osDelay(5);
           if(0 == l%10)
           {
            break;
           }
           l++;
        }
        cm_demo_printf("test_virt_at_len:%d,virt_at_ret_flag:%d\n",test_virt_at_len,virt_at_ret_flag);
        virt_at_ret_flag = 0; 
        cm_demo_printf("%s",test_virt_at_buffer);
        cm_heap_stats_t stats = {0};
        int ret = cm_mem_get_heap_stats(&stats);
        if(0 == ret)
        {
            cm_demo_printf("cm_mem_get_heap_stats RET:%d\n",ret);
            cm_demo_printf("[mem]Total Size:%d,Free Size:%d,Allocated Size:%d\n",stats.total_size,stats.free,stats.allocated);
        }
    }
    else if(0 == strcmp("SENDTEXT",cmd[2]))
    {
        if(0 == strcmp("STR",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[300] = {0};
            strlcpy(atCmgs, cmd[4],12);
            /*含有123456789字符串的数据可能收不到*/
            //strlcpy(msgData,"|!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend",  sizeof("|!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend"));
            strlcpy(msgData,"|!\"#$%&'()*+,-./245689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend",  sizeof("|!\"#$%&'()*+,-./245689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend"));
            int simid = atoi(cmd[5]);
            cm_demo_printf("atCmgs:%s, textdata:%s\n",atCmgs,msgData);
            int sendmsg_oc = cm_virt_at_sms_send(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_sms_send ret:%d\n",sendmsg_oc);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sendmsg_oc,cm_virt_at_sms_send);
        }
        else if (0 == strcmp("ASCALL",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[500] = {0};
            strlcpy(atCmgs, cmd[4],12);
            strlcpy(msgData,"323335363839306162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a636363636363636363636363633830656e64",  sizeof("323335363839306162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a636363636363636363636363633830656e64"));
            int simid = atoi(cmd[5]);
            int sendmsg_oc = cm_virt_at_sms_send(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_sms_send ret:%d\n",sendmsg_oc);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sendmsg_oc,cm_virt_at_sms_send);
        }
        else if (0 == strcmp("UCS2",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[500] = {0};
            strlcpy(atCmgs, cmd[4],12);
            strlcpy(msgData,"00008FD90000662F00004E000000676100006D4B00008BD5000077ED00004FE10000FF0C0000518500005BB9000053050000542B0000201C00004E2D000065870000201D0000FF0C0000201C000000630000006800000061000000720000201D0000FF0C0000201C0000657000005B5700000033000000360000003800000039000000350000201D0000FF0C0000727900006B8A00005B5700007B260000201C000030020000FF0C0000002A000000260000202600002026000000250000FFE500000023000000400000FF010000002B0000003D0000002D0000007E0000201D",  sizeof("00008FD90000662F00004E000000676100006D4B00008BD5000077ED00004FE10000FF0C0000518500005BB9000053050000542B0000201C00004E2D000065870000201D0000FF0C0000201C000000630000006800000061000000720000201D0000FF0C0000201C0000657000005B5700000033000000360000003800000039000000350000201D0000FF0C0000727900006B8A00005B5700007B260000201C000030020000FF0C0000002A000000260000202600002026000000250000FFE500000023000000400000FF010000002B0000003D0000002D0000007E0000201D"));
            int simid = atoi(cmd[5]);
            int sendmsg_oc = cm_virt_at_sms_send(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_sms_send ret:%d\n",sendmsg_oc);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sendmsg_oc,cm_virt_at_sms_send);
        }
        else
        {
            cm_demo_printf("param error\n");
        }

    }
    else if(0 == strcmp("SENDPDU",cmd[2]))
    {
        int atCmgs[40] = {0};
        char msgData[500] = {0};
        strlcpy(atCmgs, "125",sizeof("125"));
        /*这是一条测试短信，内容包含“中文”，“char”，“数字1234567890”，特殊字符“。，*&……%￥#@！+=-~”*/
        //strlcpy(msgData,"0001000D91688188332813F700087A8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570031003200330034003500360037003800390030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D",  sizeof("0001000D91688188332813F700087A8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570031003200330034003500360037003800390030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D"));
        /*这是一条测试短信，内容包含“中文”，“char”，“数字4680”，特殊字符“。，*&……%￥#@！+=-~”*/
        strlcpy(msgData,"0891683108200305F011000D91688188332813F70008AA6E8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570034003600380030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D",  sizeof("0891683108200305F011000D91688188332813F70008AA6E8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570034003600380030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D"));
        int simid = atoi(cmd[3]);
        int sendmsg_oc = cm_virt_at_sms_send(atCmgs,msgData,simid);
        cm_demo_printf("cm_virt_at_sms_send ret:%d\n",sendmsg_oc);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sendmsg_oc,cm_virt_at_sms_send);
    }
    else if(0 == strcmp("SAVE",cmd[2]))
    {
        if(0 == strcmp("PDU",cmd[3]))
        {
            int atCmgs[40] = {0};
            char msgData[500] = {0};
            strlcpy(atCmgs, "125",sizeof("125"));
            /*这是一条测试短信，内容包含“中文”，“char”，“数字1234567890”，特殊字符“。，*&……%￥#@！+=-~”*/
            //strlcpy(msgData,"0001000D91688188332813F700087A8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570031003200330034003500360037003800390030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D",  sizeof("0001000D91688188332813F700087A8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570031003200330034003500360037003800390030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D"));
            /*这是一条测试短信，内容包含“中文”，“char”，“数字4680”，特殊字符“。，*&……%￥#@！+=-~”*/
            strlcpy(msgData,"0891683108200305F011000D91688188332813F70008AA6E8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570034003600380030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D",  sizeof("0891683108200305F011000D91688188332813F70008AA6E8FD9662F4E0067616D4B8BD577ED4FE1FF0C51855BB95305542B201C4E2D6587201DFF0C201C0063006800610072201DFF0C201C65705B570034003600380030201DFF0C72796B8A5B577B26201C3002FF0C002A0026202620260025FFE500230040FF01002B003D002D007E201D"));
            int simid = atoi(cmd[4]);
            int virt_at_msg_memory = cm_virt_at_msg_memory(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_msg_memory ret:%d\n",virt_at_msg_memory);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,virt_at_msg_memory,cm_virt_at_msg_memory);
        }
        else if(0 == strcmp("STR",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[300] = {0};
            strlcpy(atCmgs, cmd[4],12);
            /*含有123456789字符串的数据可能收不到*/
            //strlcpy(msgData,"|!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend",  sizeof("|!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend"));
            strlcpy(msgData,"|!\"#$%&'()*+,-./245689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend",  sizeof("|!\"#$%&'()*+,-./245689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]|^_0abcdefghijklmnopqrstuvwxyz{||}~\\a \\b\\r\\n\\\\O\\0Xend"));
            int simid = atoi(cmd[5]);
            int virt_at_msg_memory = cm_virt_at_msg_memory(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_msg_memory ret:%d\n",virt_at_msg_memory);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,virt_at_msg_memory,cm_virt_at_msg_memory);
        }
        else if (0 == strcmp("ASCALL",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[500] = {0};
            strlcpy(atCmgs, cmd[4],12);
            strlcpy(msgData,"323335363839306162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a636363636363636363636363633830656e64",  sizeof("323335363839306162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a636363636363636363636363633830656e64"));
            int simid = atoi(cmd[5]);
            int virt_at_msg_memory = cm_virt_at_msg_memory(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_msg_memory ret:%d\n",virt_at_msg_memory);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,virt_at_msg_memory,cm_virt_at_msg_memory);
        }
        else if (0 == strcmp("UCS2",cmd[3]))
        {
            char atCmgs[40] = {0};
            char msgData[500] = {0};
            strlcpy(atCmgs, cmd[4],12);
            strlcpy(msgData,"00008FD90000662F00004E000000676100006D4B00008BD5000077ED00004FE10000FF0C0000518500005BB9000053050000542B0000201C00004E2D000065870000201D0000FF0C0000201C000000630000006800000061000000720000201D0000FF0C0000201C0000657000005B570000003300000036",  sizeof("00008FD90000662F00004E000000676100006D4B00008BD5000077ED00004FE10000FF0C0000518500005BB9000053050000542B0000201C00004E2D000065870000201D0000FF0C0000201C000000630000006800000061000000720000201D0000FF0C0000201C0000657000005B570000003300000036"));
            int simid = atoi(cmd[5]);
            int virt_at_msg_memory = cm_virt_at_msg_memory(atCmgs,msgData,simid);
            cm_demo_printf("cm_virt_at_msg_memory ret:%d\n",virt_at_msg_memory);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,virt_at_msg_memory,cm_virt_at_msg_memory);
        }
        else
        {
            cm_demo_printf("param error\n");
        }
    }

    else{
        cm_demo_printf("param error!\n");
    }
}


